package com.cgbl.cg.ui

import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.FilterAdapter
import com.cgbl.cg.adapter.PhotoAdapter
import com.cgbl.cg.databinding.AppActivityReportBinding
import com.cgbl.cg.view.ImagePop
import com.cgbl.cg.vm.ReportViewModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.util.ChoiceImgHelper

@Route(path = AppConstants.Router.Main.A_REPORT)
class ReportActivity : GLBaseActivity<AppActivityReportBinding, ReportViewModel>() {
    private lateinit var filterAdapter: FilterAdapter
    private lateinit var photoAdapter: PhotoAdapter
    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_report

    override fun initVariableId(): Int = BR.viewModel

    var id = ""
    override fun initComponents() {
        addSingleTitleBar("举报")
        initAdapter()
    }

    override fun initParam() {
        super.initParam()
        id = intent.getStringExtra("id") ?: ""
    }

    override fun initData() {
        super.initData()
        viewModel.id = id
    }

    private fun initAdapter() {
        filterAdapter = FilterAdapter(viewModel.select)
        binding.mRvType.apply {
            adapter = filterAdapter
        }
        filterAdapter.setNewInstance(viewModel.typeList)
        filterAdapter.setOnItemClickListener { adapter, view, position ->
            viewModel.select = filterAdapter.data[position]
        }

        photoAdapter = PhotoAdapter(R.mipmap.app_ic_photo_add_a7a7a7)
        binding.mRvImg.apply {
            adapter = photoAdapter
        }
        photoAdapter.addData("")
        photoAdapter.addChildClickViewIds(R.id.item_iv_img, R.id.item_iv_close)

        photoAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_iv_img -> {
                    if (TextUtils.isEmpty(photoAdapter.data[position])) {
                        ChoiceImgHelper.ChoiceImg(this, 9, {
                            it.forEach {
                                viewModel.doUpload(it?.fileName ?: "", it?.compressPath ?: "") {
                                    photoAdapter.addData(0, it)
                                }

                            }
                        }, {

                        })
                    } else {
                        val list = mutableListOf<String>()
                        photoAdapter.data.forEach {
                            if (!TextUtils.isEmpty(it))
                                list.add(it)
                        }
                        val imgPop = ImagePop(this)
                            .isInfinite(true)
                            .isShowSaveButton(false)
                            .setXPopupImageLoader(SmartGlideImageLoader())
                            .setSrcView(view.findViewById(R.id.item_iv_img), position)
                            .setImageUrls(list.toMutableList())
                        XPopup.Builder(this)
                            .asCustom(imgPop)
                            .show()
                    }
                }
                R.id.item_iv_close -> {
                    viewModel.imgList.remove(photoAdapter.data[position])
                    photoAdapter.data.removeAt(position)
                }
            }
        }
    }
}